package com.jf.car.service.impl;



import com.jf.car.bean.Department;
import com.jf.car.bean.Userinfo;
import com.jf.car.dao.DepartmentDao;
import com.jf.car.dao.UserinfoDao;
import com.jf.car.dao.impl.DepartmentDaoImpl;
import com.jf.car.dao.impl.UserinfoDaoImpl;
import com.jf.car.service.DepartmentService;
import com.jf.car.util.PageInfo;
import com.jf.car.util.ResultJson;

import java.util.List;

public class DempartmenterviceImpl implements DepartmentService {
    private DepartmentDao departmentDao = new DepartmentDaoImpl();
    private UserinfoDao userinfoDao = new UserinfoDaoImpl();
    @Override
    public List<Department> getAll() {
        return departmentDao.getAll();
    }

    @Override
    public ResultJson getSelect() {
        return ResultJson.success(departmentDao.getAll());//list集合放入data属性中
    }

    @Override
    public ResultJson getPage(PageInfo pageInfo, Department department) {
        List<Department> list = departmentDao.getPage(pageInfo,department);
        int count = departmentDao.getCount(department);
        pageInfo.setPageData(list,count);
        return ResultJson.success(pageInfo);
    }

    @Override
    public ResultJson saveOrUpdate(Department department) {
        int result;
        if (department.getId() == null){
            result = departmentDao.addSelect(department);
        }else {
            result = departmentDao.updateSelect(department);
        }
        return result > 0 ?ResultJson.success():ResultJson.error("操作失败");
    }

    @Override
    public ResultJson delete(int[] ids) {
        List<Userinfo> list = userinfoDao.getUserinfoByDepartmentIds(ids);
        if (!list.isEmpty()){
            return ResultJson.error("编号【"+list.get(0).getDepartmentId()+"】已被使用,无法删除");
        }
        int result = departmentDao.batchDelete(ids);
        return result == ids.length?ResultJson.success():ResultJson.error("删除异常");
    }

    @Override
    public ResultJson info(int id) {
        return ResultJson.success(departmentDao.getById(id));
    }
}
